package com.example.practice.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.net.ssl.HttpsURLConnection;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.ColoursXYSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import org.json.JSONObject;

import android.R.integer;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.example.practice.bean.ChartPagerBean;


public class Chart  {
	
	//填写已下代码 time series
	public GraphicalView chart(Context context,ChartPagerBean bean,LinearLayout view) {
			XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
			XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
			GraphicalView graphicalView;

			//init renderer
			renderer.setAxesColor(Color.BLACK);
			renderer.setAxisTitleTextSize(25);
			renderer.setLegendTextSize(25);
			renderer.setPointSize(10);
			renderer.setZoomButtonsVisible(true);
			renderer.setMargins(new int[]{0,20,0,20});
			renderer.setLabelsTextSize(18);
			renderer.setYLabelsColor(0, Color.WHITE);
			renderer.setXLabelsColor(Color.WHITE);
			
			int yMax = bean.majorMax;
			int yMin = bean.majorMin;
			
		
			//set major series
			XYSeries majorSeries = new XYSeries(bean.majorName);
			for (int i = 0; i < bean.majorValueList.size(); i++) {
				int value = bean.majorValueList.get(i);
				majorSeries.add(i+1, value);
				yMax = yMax > value? yMax : value;
				yMin = yMin < value? yMin : value;
			}
			
			dataset.addSeries(majorSeries);
			
			//set major renderer
			ColoursXYSeriesRenderer majorRenderer = new ColoursXYSeriesRenderer();
			majorRenderer.setChartValuesSpacing(15);
			majorRenderer.setChartValueTextColor(Color.WHITE);
			majorRenderer.setColor(Color.BLUE);
			majorRenderer.setPointColor(Color.GREEN);
			majorRenderer.setPointStyle(PointStyle.CIRCLE);
			majorRenderer.setFillPoints(true);
			majorRenderer.setUseColor(true);
			majorRenderer.setDisplayChartValues(true);
			majorRenderer.setDisplayChartValuesDistance(1);
			majorRenderer.setChartValuesTextSize(20);
			majorRenderer.setWarningMaxValue(bean.majorWarningMax);
			majorRenderer.setWarningMinValue(bean.majorWarningMin);
			
			renderer.addSeriesRenderer(majorRenderer);

			
			//set max and min
			renderer.setXAxisMin(0);
			renderer.setXAxisMax(10);
			renderer.setYAxisMax(yMax);
			renderer.setYAxisMin(yMin);
			
			graphicalView = ChartFactory.getLineChartView(context, dataset, renderer);
			
			//set transparent background
			renderer.setBackgroundColor(Color.TRANSPARENT);
			renderer.setMarginsColor(Color.TRANSPARENT);
			renderer.setApplyBackgroundColor(true);
			renderer.setSelectableBuffer(10);
			
			
			if(view != null){
				view.removeAllViews();
				view.addView(graphicalView);
			}
			
			return graphicalView;
	}
	


	private String getResult(String path,JSONObject params) {
		BufferedReader bufferedReader = null;
		OutputStream outputStream = null;
		String result = "";
		try {
			URL url =  new URL(path);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setRequestMethod("post");
			connection.setRequestProperty("Content-Type", "application/json");
			outputStream = connection.getOutputStream();
			outputStream.write(params.toString().getBytes());
			outputStream.flush();
			
			bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			if(connection.getResponseCode() == 200){
				while((line = bufferedReader.readLine()) != null){
					result += line;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(outputStream!=null){
					outputStream.close();
				}
				if(bufferedReader!=null){
					bufferedReader.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
		JSONObject jsonObject = new JSONObject();
		return result;
	}
} 
